package com.hainatong.shiphelper;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;
import com.hainatong.shiphelper.api.ApiResult;
import com.hainatong.shiphelper.api.CanBoService;
import com.hainatong.shiphelper.login.entity.UserResult;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.http.ApiBuildFactory;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class TestViewModel extends BaseViewModel
{

    public final ObservableField<String> username = new ObservableField<>();

    public TestViewModel(@NonNull Application application)
    {
        super(application);
    }



    public MutableLiveData<Boolean>  changeText()
    {
        final MutableLiveData<Boolean> mTx = new MutableLiveData<>();


        Observable.just(1000)
                  .timer(1500, TimeUnit.MILLISECONDS)
                  .compose(RxSchedulersUtils.Obs_io_main())
                  .subscribe( (s)->{
                      mTx.setValue( true );
                  } );

        return mTx;
    }



    public MutableLiveData<String> getInfoByNet(String param )
    {
        MutableLiveData<String> mResult = new MutableLiveData<>();
        Map<String,String> params = new HashMap<>();
        params.put("username","user01");
        params.put("password","123");
        execute(ApiBuildFactory.getInstance().create(CanBoService.class, CanBoService.BASE_URL)
                .login(params), new Observer<ApiResult<UserResult>>() {
            @Override
            public void onSubscribe(Disposable d) {
                addDisposable(d);
            }

            @Override
            public void onNext(ApiResult<UserResult> s) {
                mResult.setValue( s.toString() );
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });


        /*
        ApiService.Builder.getDouBanService().getCurrentUser()
                          .compose(RxSchedulersUtils.Obs_io_main())
                          .subscribe(new Observer<String>() {
                              @Override
                              public void onSubscribe(Disposable d)
                              {
                                addDisposable(d);
                              }

                              @Override
                              public void onNext(String result)
                              {
                                  mResult.setValue( result );
                              }

                              @Override
                              public void onError(Throwable e)
                              {

                              }

                              @Override
                              public void onComplete()
                              {

                              }
                          });
        */

        return mResult;
    }


}
